import type {
    CheckboxGroupProps,
    FormItemProps,
    InputNumberProps,
    InputProps,
    RadioProps,
    SelectProps,
    TextAreaProps
} from "ant-design-vue";
import type { RuleObject } from "ant-design-vue/es/form/interface";

// 支持的组件类型（可根据实际需要扩展）
type ComponentType =
    | "input"
    | "input-number"
    | "input-password"
    | "select"
    | "date-picker"
    | "date-range-picker"
    | "textarea"
    | "checkbox"
    | "radio";

// 表单项配置通用接口
interface FormItemBase<T = InputProps> extends FormItemProps {
    /** 字段名，对应 form 的 key */
    name: string; // 虽然 antdv 类型 FormItemProps 中已经包括了，这里重复定义，为了表示 name 和 label 是必传的，另外 name 字段值必须是接口 DTO 的字段名
    /** 标签名 */
    label: string;
    /** 组件类型 */
    type: ComponentType;
    /** 表单校验规则 */
    rules?: RuleObject[];
    /** 传递给组件（如 a-input）的额外属性 */
    componentProps?: T;
}

// 使用映射类型或联合类型来关联 type 和具体的表单项组件 props
export type FormItemConfig<DTO = Record<string, any>> =
    | (FormItemBase<InputProps> & { type: "input"; name: keyof DTO })
    | (FormItemBase<InputNumberProps> & { type: "input-number"; name: keyof DTO })
    | (FormItemBase<InputProps> & { type: "input-password"; name: keyof DTO })
    | (FormItemBase<TextAreaProps> & { type: "textarea"; name: keyof DTO })
    | (FormItemBase<SelectProps> & { type: "select"; name: keyof DTO })
    // | (FormItemBase<DatePickerProps> & { type: "date-picker"; name: keyof DTO })
    // | (FormItemBase<RangePickerProps> & { type: "date-range-picker"; name: keyof DTO })
    | (FormItemBase<any> & { type: "date-picker"; name: keyof DTO })
    | (FormItemBase<any> & { type: "date-range-picker"; name: keyof DTO })
    | (FormItemBase<CheckboxGroupProps> & {
          type: "checkbox";
          name: keyof DTO;
          componentProps: { options: Array<{ label: string; value: string }> };
      })
    | (FormItemBase<RadioProps> & {
          type: "radio";
          name: keyof DTO;
          componentProps: { options: Array<{ label: string; value: string }> };
      });
